package com.zonda.sampleretrofit2.model;

import android.text.TextUtils;
import android.util.JsonReader;

import java.io.IOException;
import java.util.ArrayList;

public final class AppNewsListResult extends AppBasicResult{

  private ArrayList<NewsModel> listDatas;

  @Override
  protected boolean readDataObject(JsonReader jsonReader) throws IOException {
    super.readDataObject(jsonReader);
    jsonReader.beginObject();
    while (jsonReader.hasNext()) {
      final String name = jsonReader.nextName();
      if (TextUtils.equals(name, "articles")) {
        readArticlesArray(jsonReader);
      } else {
        jsonReader.skipValue();
      }
    }
    jsonReader.endObject();
    return true;
  }

  private void readArticlesArray(JsonReader jsonReader) throws IOException {

    jsonReader.beginArray();
    listDatas = new ArrayList<>();
    NewsModel listItemModel;
    while (jsonReader.hasNext()) {
      listItemModel = new NewsModel();
      listItemModel.convertFrom(jsonReader);
      listDatas.add(listItemModel);
    }
    jsonReader.endArray();
  }

  public ArrayList<NewsModel> getListDatas() {
    return listDatas;
  }

  public void setListDatas(ArrayList<NewsModel> listDatas) {
    this.listDatas = listDatas;
  }
}
